package oylj.recycle.all_in_one.face;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;

import oylj.recycle.all_in_one.R;

public class OverlayView extends View {
    private static final String TAG = "OverlayView";
    private Paint transparentPaint;
    private Paint borderPaint;
    private RectF oval;
    private float progress = 100;
    private int borderColor = Color.RED;
    private final int borderWidth = 10;
    private Runnable progressRunnable;
    private final Handler handler = new Handler();
    private boolean isSuccess = false;
    private boolean showIcon = false;
    private int radius;
    private int centerY;


    public int getCenterY() {
        return centerY;
    }
    public int getRadius() {
        return radius;
    }
    public float getProgress() {
        return progress;
    }
    public OverlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init() {
        // 用于绘制透明的圆形区域
        transparentPaint = new Paint();
        transparentPaint.setAntiAlias(true);
        transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); // 透明

        // 用于绘制边框进度条
        borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(borderWidth);
        borderPaint.setColor(borderColor);

        // 定义一个矩形用于绘制圆弧（边框）
        oval = new RectF();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 获取视图的中心和半径
        int viewWidth = getWidth();
        int viewHeight = getHeight();
        radius = Math.min(viewWidth, viewHeight) / 3; // 根据需要调整圆的大小

        int centerX = viewWidth / 2;
        centerY = viewHeight / 3;

        // 绘制背景，除了圆形区域，其他地方变暗（灰色）
//        canvas.drawColor(Color.parseColor("#80000000"));
        canvas.drawColor(Color.parseColor("#ffffffff"));

        // 绘制透明的圆形区域，让相机预览透过
        canvas.drawCircle(centerX, centerY, radius, transparentPaint);

        float halfBorderWidth = borderWidth / 2f; // 边框的一半宽度
        oval.set(centerX - radius - halfBorderWidth, centerY - radius - halfBorderWidth,
                centerX + radius + halfBorderWidth, centerY + radius + halfBorderWidth);

        // 绘制圆形边框进度条
        canvas.drawArc(oval, 90, (progress * 360) / 100, false, borderPaint);

        if (showIcon ) {
            Drawable svgDrawable = ContextCompat.getDrawable(getContext(),isSuccess? R.drawable.success_icon: R.drawable.warn_icon);
            int imageSize = radius/2; // 正方形边长等于圆形半径

            // 计算正方形图片的左上角坐标
            int left = centerX + radius - imageSize;
            int top = centerY + radius - imageSize;
            svgDrawable.setBounds(left, top, left + imageSize+borderWidth, top + imageSize+borderWidth);
            svgDrawable.draw(canvas);
        }
    }
    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    // 设置进度
    public void setProgress(float progress) {
        this.progress = progress;
        invalidate(); // 重绘视图
    }

    // 设置边框颜色
    public void setBorderColor(int color) {
        this.borderColor = color;
        borderPaint.setColor(color);
        invalidate(); // 重绘视图
    }
    public void setProgressIncrementally(float targetProgress, int duration) {
        // 取消当前正在执行的进度任务
        if (progressRunnable != null) {
            handler.removeCallbacks(progressRunnable);
        }

        // 计算每次更新的步长和间隔时间
        float currentProgress = progress;
        int steps = (int) (duration / 10);  // 100ms 更新一次
        float increment = (float) (targetProgress - currentProgress) / steps; // 每次递增的步长

        progressRunnable = new Runnable() {
            @Override
            public void run() {
                // 如果达到目标进度或者步骤已经完成，停止更新
                if (progress >= targetProgress) {
                    if(  targetProgress == 100){
                        showIcon = true;
                        invalidate();
                    }
                    return;
                }
                progress += increment;
                progress = Math.min(progress,100);
                invalidate();
                handler.postDelayed(this, 10);
            }
        };

        // 启动任务
        handler.post(progressRunnable);
    }


}
